package com.dao.impl;

import com.dao.IEnterpriseDao;
import com.pojo.Enterprise;
import com.util.DBHelper;

import java.util.ArrayList;

public class EnterpriseDaoImpl implements IEnterpriseDao {
    private DBHelper db = new DBHelper();

    // 1.新增企业（企业信息认证）
    @Override
    public int insertEnterprise(Enterprise enterprise) {
        String sql = "insert into enterprise values(null,?, ?, ?, ?, ?, ?, ?)";
        Object[] objs = {enterprise.getName(), enterprise.getSocialUniformCode(),
                enterprise.getEmail(), enterprise.getPhone(), enterprise.getAddress(),
                enterprise.getScale(), enterprise.getFax()};
        return db.dtUpdate(sql, objs);
    }

    // 2.企业查重名称+统一社会信用代码）
    @Override
    public boolean checkEnterprise(String name, String socialUniformCode) {
        String sql = "select * from enterprise where name = ? or socialUniformCode = ?";
        ArrayList<Enterprise> list = db.dtQuery(sql, Enterprise.class, name, socialUniformCode);
        return !list.isEmpty();
    }

    // 3.根据ID查企业
    @Override
    public Enterprise selectEnterpriseById(Integer id) {
        String sql = "select * from enterprise where id = ?";
        ArrayList<Enterprise> list = db.dtQuery(sql, Enterprise.class, id);
        return list.size() > 0 ? list.get(0) : null;
    }

    // 根据企业名称和统一社会信用代码查询企业
    @Override
    public Enterprise selectEnterpriseByNameAndCode(String name, String socialUniformCode) {
        String sql = "select * from enterprise where name = ? and socialUniformCode = ?";
        ArrayList<Enterprise> list = db.dtQuery(sql, Enterprise.class, name, socialUniformCode);
        return list.size() > 0 ? list.get(0) : null;
    }



    // 5.查可添加黑名单的企业（排除自身、已有黑名单）
    @Override
    public ArrayList<Enterprise> selectBlackListAvailable(Integer enterpriseId) {
        String sql = "select * from enterprise " +
                "where id not in (select supplierId from supplierBlackList where enterpriseId = ?)";
        return db.dtQuery(sql, Enterprise.class,enterpriseId);
    }

    //6.在白名单根据用户输入的企业名称模糊查询
    public ArrayList<Enterprise> queryEnterpriseByLikeName(String name) {
        String sql = "select * from enterprise where name like ?";
        return db.dtQuery(sql, Enterprise.class, "%" + name + "%");
    }

    //全查
    @Override
    public ArrayList<Enterprise> queryAllEnterprises() {
        String sql = "select * from enterprise";
        return db.dtQuery(sql, Enterprise.class);
    }

}
